<?php

namespace app\helps;

use app\facade\Redis;

class Auth
{
    private $config = [
        'time_line' => 86400 * 1,
    ];

    public function set($user_id, $prefix = 'login_')
    {
        $token = md5(uniqid() . time() . $user_id);
        Redis::hset($prefix."uid", $user_id,$token);
        return Redis::setex($prefix . $token, $this->config['time_line'], $user_id) ? $token : false;
    }

    public function get($token, $prefix = 'login_')
    {
        $userId = Redis::get($prefix . $token);
        $uidToken = Redis::hget($prefix."uid", $userId);
        if ($uidToken&&$uidToken == $token) {
            Redis::expire($prefix . $token, $this->config['time_line']);
            return $userId ?: false;
        }
        return false;
    }

    public function del($token)
    {
        return Redis::hDel($token);
    }
}